var quanxuan = document.getElementById("quanxuan");
var gouxuan = document.getElementsByClassName("gouxuan");
var danjia = document.getElementsByClassName("danjia");
var shuliang = document.getElementsByClassName("shu");
var zongjia = document.getElementsByClassName("yuan");
var spsl = document.getElementById("spsl");
var zhifu = document.getElementById("spzj");
var jian = document.getElementsByClassName("jian");
var jia = document.getElementsByClassName("jia");
var shengcaibox = document.getElementById("shengcaibox");
var shengcai = document.getElementsByClassName("shengcai");

//点击勾选会改变spsl商品数量以及zhifu支付金额
for (var i = 0; i < gouxuan.length; i++) {
	gouxuan[i].zdygx = i;
	gouxuan[i].onclick = function() {
		if (gouxuan[this.zdygx].checked == true) {
			var number = parseInt(shuliang[this.zdygx].innerHTML);
			var number1 = parseInt(spsl.innerHTML);
			if (spsl.innerHTML == 0) {
				spsl.innerHTML = "";
			}
			spsl.innerHTML = number1 + number;
			var number3 = parseInt(danjia[this.zdygx].innerHTML);
			var number4 = parseInt(shuliang[this.zdygx].innerHTML);
			var number5 = parseInt(zhifu.innerHTML);
			if (zhifu.innerHTML == 0) {
				zhifu.innerHTML = "";
			}
			zhifu.innerHTML = number5 + number3 * number4;
		}
		if (gouxuan[this.zdygx].checked == false) {
			var number = parseInt(shuliang[this.zdygx].innerHTML);
			var number1 = parseInt(spsl.innerHTML);
			if (spsl.innerHTML == 0) {
				spsl.innerHTML = "";
			}
			spsl.innerHTML = number1 - number;
			var number3 = parseInt(danjia[this.zdygx].innerHTML);
			var number4 = parseInt(shuliang[this.zdygx].innerHTML);
			var number5 = parseInt(zhifu.innerHTML);
			if (zhifu.innerHTML == 0) {
				zhifu.innerHTML = "";
			}
			zhifu.innerHTML = number5 - number3 * number4;
		}

	}
}

//点击全选会实现全部被勾选
quanxuan.onclick = function(){
	var cun = 0;
	for (var i = 0;i < gouxuan.length;i++) {
		if(gouxuan[i].checked == true){
			cun++;
		}
	}
	for (var i = 0;i < gouxuan.length;i++ ) {
		if(cun == gouxuan.length){
			gouxuan[i].checked = false;
			quanxuan.checked = false;
		}else{
			gouxuan[i].checked = true;
			quanxuan.checked = true;
			spsl.innerHTML = 0;
			zhifu.innerHTML = 0;
		}
	}

	//全选后总价与数量会相应改变
	for (var i = 0;i < gouxuan.length;i++) {
		if(gouxuan[i].checked == true){
		var num1 = parseInt(spsl.innerHTML);
		var num2 = parseInt(shuliang[i].innerHTML);
		spsl.innerHTML = num1 + num2;
		zhifu.innerHTML = Number(zongjia[i].innerHTML) + Number(zhifu.innerHTML);
	}	if(gouxuan[i].checked == false){
		spsl.innerHTML = Number(spsl.innerHTML) - Number(shuliang[i].innerHTML);
		zhifu.innerHTML = Number(zhifu.innerHTML) - Number(zongjia[i].innerHTML);
	}		
	}

}


//大图div轮播
//定义shengcaibox的宽度
shengcaibox.style.width = shengcai.length * 170 + "px";
function lunbo() {
	var t = 0;
	var b = shengcaibox.offsetLeft;
	var c = 5 * -170;
	var d = 500;
	var timer = setInterval(function() {
		t++;
		if (t >= d) {
			clearInterval(timer);
		}
		shengcaibox.style.left = Tween.Linear(t, b, c, d) + "px";
	}, 50)
};
lunbo();
setInterval(function() {
	shengcaibox.style.left = "0px";
	lunbo();
}, 25000)

//点击-和+会对应增加或减少商品数量,且改变后面的价格
for (var i = 0; i <= jian.length; i++) {
	jian[i].zdy = i;
	jia[i].zidingyi = i;
	jia[i].onclick = function() {
		if(shuliang[this.zidingyi].innerHTML < 4){
		shuliang[this.zidingyi].innerHTML++;
		if (shuliang[this.zidingyi].innerHTML >= 4) {
			shuliang[this.zidingyi].innerHTML = 4;
		}
		var num1 = parseInt(danjia[this.zidingyi].innerHTML);
		var num2 = parseInt(shuliang[this.zidingyi].innerHTML);
		zongjia[this.zidingyi].innerHTML = num1 * num2;
		//实现点击 + 的时候，总数量和需支付总价同步变化
		if (gouxuan[this.zidingyi].checked == true) {
			var number1 = parseInt(spsl.innerHTML);
			
			spsl.innerHTML = number1 + 1;
			if (spsl.innerHTML <= 0) {
				spsl.innerHTML = 0;
			}
			var number3 = parseInt(danjia[this.zidingyi].innerHTML);
			var number5 = parseInt(zhifu.innerHTML);
			
			zhifu.innerHTML = number5 + number3;
			if (zhifu.innerHTML <= 0) {
				zhifu.innerHTML = 0;
			}
		}
		}
	}
	
	jian[i].onclick = function() {
		if(shuliang[this.zdy].innerHTML > 0){
		shuliang[this.zdy].innerHTML--;
		if (shuliang[this.zdy].innerHTML <= 0) {
			shuliang[this.zdy].innerHTML = 0;
		}
		var num1 = parseInt(danjia[this.zdy].innerHTML);
		var num2 = parseInt(shuliang[this.zdy].innerHTML);
		zongjia[this.zdy].innerHTML = num1 * num2;
		
		
		//实现点击- 号的时候，商品数量和需支付价格同步变化
		if (gouxuan[this.zdy].checked == true ) {
			var number1 = parseInt(spsl.innerHTML);
			spsl.innerHTML = number1 - 1;	
			if (spsl.innerHTML <= 0) {
				spsl.innerHTML = 0;
			}
			var number3 = parseInt(danjia[this.zdy].innerHTML);
			var number5 = parseInt(zhifu.innerHTML);	
			zhifu.innerHTML = number5 - number3;
			if (zhifu.innerHTML <= 0) {
				zhifu.innerHTML = 0;
			}
			
	
		}
		}
	}
}